#ifndef TCPCLIENT_H
#define TCPCLIENT_H

#include <QObject>
#include <QTcpSocket>
#include <QMap>
#include <QVector>
#include <QVariant>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlError>
#include <QNetworkProxy>
#include "log.h"

// 单个 TCP 客户端连接类
class TCPClient : public QObject
{
    Q_OBJECT
public:
    explicit TCPClient(QObject *parent = nullptr);
    void connectToServer(const QString& host, int port, int key);

    QTcpSocket *tcpSocket;
    int clientKey;

signals:
    void serverDataReceived(int index, const QByteArray& data);

private slots:
    void onReadyRead();
    void onSocketError(QAbstractSocket::SocketError socketError);
private:
    Log* m_logger;
};

// 管理所有 TCP 客户端连接
class TCPClientManager : public QObject
{
    Q_OBJECT
public:
    explicit TCPClientManager(QObject *parent = nullptr);

private:
    QMap<int, TCPClient*> clients;
    QMap<int, QVector<QVariant>> readDatabase(QSqlDatabase& db);
    void setupTcpClient(int key, QVector<QVariant> row);
    Log* m_logger;
};

#endif // TCPCLIENT_H
